//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public abstract class NumericDataSource : DataSource
    {
        public abstract IEnumerable<NumericValue> NumericValues { get; }

        public override IEnumerable<object> Values
        {
            get { foreach (NumericValue o in NumericValues) yield return o; }
        }

        public event EventHandler ValueChanged;
        private bool inEdit;

        protected void EndEdit()
        {
            inEdit = false;
        }

        protected void BeginEdit()
        {
            inEdit = true;
        }


        protected internal void OnValueChanged()
        {
            if (inEdit)
                return;
            if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
            NotifyPropertyChanged("Values");
            NotifyPropertyChanged("NumericValues");
        }

        internal static NumericDataSource ReadOptionalNumericDataSource(string name, ReadContext context)
        {
            if (context.ReaderLocalName != name)
                return null;
            return ReadNumericDataSource(name, context);
        }

        internal static NumericDataSource ReadNumericDataSource(string name, ReadContext context)
        {
            if (context.ReaderLocalName != name)
                return null;
            using (var c = context.Read(name))
            {
                switch (c.ReaderLocalName)
                {
                    case "numRef":
                        return NumericReference.ReadNumericReference(c);
                    case "numLit":
                        return c.ReadElement<NumericLiteral>(); //\\"numLit");
                    default:
                        throw new NotSupportedException();
                }
            }
        }
    }
}